﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PizzaSystem
{
    public partial class VendorApp : Form
    {
        public VendorApp()
        {
            InitializeComponent();
        }

        public VendorApp(AppHome app, Vendor vendor)
        {
            InitializeComponent();
            this.app = app;
            this.vendor = vendor;
            getInventoryBill();
        }

        Vendor vendor;

        AppHome app;

        SqlDao sqlDao = new SqlDao();
        bool e1 = true;
        private void getInventoryBill()
        {
            DataSet ds = sqlDao.selectInventoryBill(vendor.V_id, 0);
            if (ds.Tables[0].Rows.Count>0)
            {
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
                if (e1)
                {
                    DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                    column.Text = "交 付";
                    column.Name = "编 辑";
                    column.UseColumnTextForButtonValue = true;
                    dataGridView1.Columns.Add(column);
                    e1 = false;
                }
            }
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPageIndex==0)
            {
                getInventoryBill();
            }
            else
            {
                if (MessageBox.Show("确定要退出？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    app.openForm(new Home(app));
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex==0 
                
                && MessageBox.Show("确定交付原料？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string ib_id = dataGridView1.Rows[e.RowIndex].Cells["订单编号"].Value.ToString();
                string i_id = dataGridView1.Rows[e.RowIndex].Cells["原料编号"].Value.ToString();
                string i_num = dataGridView1.Rows[e.RowIndex].Cells["数量"].Value.ToString();
                if(sqlDao.updateInventoryBillFinish(ib_id, long.Parse(i_id), int.Parse(i_num)))
                {
                    MessageBox.Show("交付成功...");
                    dataGridView1.Rows.RemoveAt(e.RowIndex);
                }
                else
                {
                    MessageBox.Show("交付失败...");
                }
            }
        }
    }
}
